﻿using JNPF.Basics.Logic;
using JNPF.Basics.Models;
using JNPF.Basics.Models.System.Dtos.Menu;
using JNPF.Utils;
using JNPF.Utils.Controller;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;

namespace JNPF.Basics.Controller.System
{
    /// <summary>
    /// 用户管理
    /// 版 本：V3.0.0
    /// 版 权：引迈信息技术有限公司（https://www.jnpfsoft.com）
    /// 作 者：JNPF开发平台组
    /// 日 期：2017.09.21 
    /// </summary>
    [Route("api/System/[controller]")]
    public class MenuController : BaseApiController
    {
        private MenuBll menuBll = new MenuBll();

        #region GET
        /// <summary>
        /// 获取菜单列表
        /// </summary>
        /// <param name="input">参数</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResponseResult> GetList(ModuleListQuery input)
        {
            var data = await menuBll.GetList();
            if (!string.IsNullOrEmpty(input.category))
            {
                data = data.FindAll(t => t.Category.Equals(input.category));
            }
            if (!string.IsNullOrEmpty(input.keyword))
            {
                data = data.TreeWhere(t => t.FullName.Contains(input.keyword) || t.EnCode.Contains(input.keyword), t => t.Id, t => t.ParentId);
            }
            var treeList = data.MapToList<ModuleListOutput>();
            return Success(new { list = treeList.ToTree("-1") });
        }

        /// <summary>
        /// 获取菜单下拉框
        /// </summary>
        /// <param name="category">菜单分类（参数有Web,App），默认显示所有分类</param>
        /// <returns></returns>
        [HttpGet("Selector")]
        public async Task<ResponseResult> GetSelector(string category)
        {
            var data = await menuBll.GetList();
            data = data.FindAll(t => t.Type == 1);
            if (!string.IsNullOrEmpty(category))
            {
                data = data.FindAll(t => t.Category.Equals(category));
            }
            var treeList = data.MapToList<ModuleSelectorOutput>();
            return Success(new { list = treeList.ToTree("-1") });
        }

        /// <summary>
        /// 获取菜单列表（下拉框）
        /// </summary>
        /// <param name="category"></param>
        /// <returns></returns>
        [HttpGet("Selector/All")]
        public async Task<ResponseResult> GetSelectorAll(string category)
        {
            var data = (await menuBll.GetList()).FindAll(x => x.EnabledMark == 1);
            if (!string.IsNullOrEmpty(category))
            {
                data = data.FindAll(t => t.Category.Equals(category));
            }
            var treeList = data.MapToList<ModuleSelectorAllOutput>();
            return Success(new { list = treeList.ToTree("-1") });
        }

        /// <summary>
        /// 获取菜单信息
        /// </summary>
        /// <param name="id">主键id</param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public async Task<ResponseResult> GetInfo(string id)
        {
            var data = await menuBll.GetInfo(id);
            var output = data.MapTo<ModuleInfoOutput>();
            return Success(output);
        }
        #endregion

        #region Post
        /// <summary>
        /// 添加菜单
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResponseResult> Creater([FromBody] ModuleCrInput input)
        {
            if (await menuBll.IsExistByEnCode(input.enCode, null))
            {
                return Error("编码已存在");
            }
            if (await menuBll.IsExistByFullName(input.fullName, null))
            {
                return Error("名称已存在");
            }
            var entity = input.MapTo<ModuleEntity>();
            entity.PropertyJson = JsonHelper.ToJson(input.propertyJson);
            var isOk = await menuBll.Create(entity);
            return isOk > 0 ? Success("新建成功") : Error("新建失败");
        }

        /// <summary>
        /// 修改菜单
        /// </summary>
        /// <param name="id"></param>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPut("{id}")]
        public async Task<ResponseResult> Update(string id, [FromBody] ModuleUpInput input)
        {
            if (await menuBll.IsExistByEnCode(input.enCode, id))
            {
                return Error("编码已存在");
            }
            if (await menuBll.IsExistByFullName(input.fullName, id))
            {
                return Error("名称已存在");
            }
            var entity = input.MapTo<ModuleEntity>();
            entity.PropertyJson = JsonHelper.ToJson(input.propertyJson);
            var isOk = await menuBll.Update(id, entity);
            return isOk > 0 ? Success("修改成功") : Error("修改失败");
        }

        /// <summary>
        /// 删除菜单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public async Task<ResponseResult> Delete(string id)
        {
            var entity = await menuBll.GetInfo(id);
            await menuBll.Delete(entity);
            return Success("删除成功");
        }

        /// <summary>
        /// 更新菜单状态
        /// </summary>
        /// <param name="id">菜单id</param>
        /// <returns></returns>
        [HttpPut("{id}/Actions/State")]
        public async Task<ResponseResult> ActionsState(string id)
        {
            var entity = await menuBll.GetInfo(id);
            entity.EnabledMark = entity.EnabledMark == 0 ? 1 : 0;
            var isOk = await menuBll.Update(id, entity);
            return isOk > 0 ? Success("更新状态成功") : Error("更新状态失败");
        }
        #endregion
    }
}
